import {
	IContext,
	IOpenViewHelper,
	IOpenViewResult,
	IParam,
	IViewDetail,
} from '@/core';
import { router } from '@/router';
import { buildUpRoutePath } from '@/utils';
import { DrawerHelper } from './drawer-helper';
import { ModalHelper } from './modal-helper';

export class OpenViewHelper implements IOpenViewHelper {
	/**
	 * @description 单例变量声明
	 * @protected
	 * @static
	 * @type {IOpenViewHelper}
	 * @memberof OpenViewHelper
	 */
	protected static service: IOpenViewHelper;

	/**
	 * @description 获取实例
	 * @static
	 * @return {*}  {IOpenViewHelper}
	 * @memberof OpenViewHelper
	 */
	static getInstance(): IOpenViewHelper {
		if (!this.service) {
			this.service = new OpenViewHelper();
		}
		return this.service;
	}

	/**
	 * @description 打开视图
	 * @param {IViewDetail} view 视图详情
	 * @param {IContext} context 应用上下文
	 * @param {IParam} viewParams 视图参数
	 * @return {*}  {(void | Promise<IOpenViewResult>)}
	 * @memberof OpenViewHelper
	 */
	openPage(
		view: IViewDetail,
		context: IContext,
		viewParams: IParam
	): void | Promise<IOpenViewResult> {
		const { openMode } = view;
		switch (openMode) {
			case 'INDEXVIEWTAB':
				return this.openIndexViewTab(view, context, viewParams);
			case 'POPUP':
				return this.openPopup(view, context, viewParams);
			case 'POPUPMODAL':
				return this.openModal(view, context, viewParams);
			case 'POPUPAPP':
				return this.openApp(view, context, viewParams);
			case 'POPOVER':
				return this.openPopover(view, context, viewParams);
			case 'DRAWER_TOP':
				return this.openTopDrawer(view, context, viewParams);
			case 'DRAWER_BOTTOM':
				return this.openBottomDrawer(view, context, viewParams);
			case 'DRAWER_LEFT':
				return this.openLeftDrawer(view, context, viewParams);
			case 'DRAWER_RIGHT':
				return this.openRightDrawer(view, context, viewParams);
			case 'USER':
				console.log('自定义模式，暂时不实现');
				break;
			default:
				return this.openIndexViewTab(view, context, viewParams);
		}
	}

	/**
	 * @description 顶级分页打开视图
	 * @param {IViewDetail} view 视图详情
	 * @param {IContext} context 应用上下文
	 * @param {IParam} viewParams 视图参数
	 * @memberof OpenViewHelper
	 */
	openIndexViewTab(
		view: IViewDetail,
		context: IContext,
		viewParams: IParam
	): void {
		const { currentRoute, push } = router;
		const routePath = buildUpRoutePath(
			currentRoute.value,
			view,
			context,
			[],
			viewParams
		);
		push(routePath);
	}

	/**
	 * @description 非模式弹出
	 * @param {IViewDetail} view 视图详情
	 * @param {IContext} context 应用上下文
	 * @param {IParam} viewParams 视图参数
	 * @return {*}  {Promise<IOpenViewResult>}
	 * @memberof OpenViewHelper
	 */
	openPopup(
		view: IViewDetail,
		context: IContext,
		viewParams: IParam
	): Promise<IOpenViewResult> {
		throw new Error('-----POPUP-----非模式弹出，暂时不实现');
	}

	/**
	 * @description 模态弹出
	 * @param {IViewDetail} view 视图详情
	 * @param {IContext} context 应用上下文
	 * @param {IParam} viewParams 视图参数
	 * @return {*}  {Promise<IOpenViewResult>}
	 * @memberof OpenViewHelper
	 */
	openModal(
		view: IViewDetail,
		context: IContext,
		viewParams: IParam
	): Promise<IOpenViewResult> {
		const modalInstance = ModalHelper.getInstance();
		return modalInstance.openModal(view, context, viewParams);
	}

	/**
	 * @description 独立程序弹出
	 * @param {IViewDetail} view 视图详情
	 * @param {IContext} context 应用上下文
	 * @param {IParam} viewParams 视图参数
	 * @return {*}  {Promise<IOpenViewResult>}
	 * @memberof OpenViewHelper
	 */
	openApp(
		view: IViewDetail,
		context: IContext,
		viewParams: IParam
	): Promise<IOpenViewResult> {
		return new Promise((resolve, reject) => {
			const { currentRoute } = router;
			const routePath = buildUpRoutePath(
				currentRoute.value,
				view,
				context,
				[],
				viewParams
			);
			window.open('./#' + routePath, '_blank');
			resolve({ ok: true, data: null });
		});
	}

	/**
	 * @description 气泡打开
	 * @param {IViewDetail} view 视图详情
	 * @param {IContext} context 应用上下文
	 * @param {IParam} viewParams 视图参数
	 * @return {*}  {Promise<IOpenViewResult>}
	 * @memberof OpenViewHelper
	 */
	openPopover(
		view: IViewDetail,
		context: IContext,
		viewParams: IParam
	): Promise<IOpenViewResult> {
		// todo 气泡打开待补充
		throw new Error('暂未实现气泡打开视图');
	}

	/**
	 * @description 打开上方抽屉
	 * @param {IViewDetail} view 视图详情
	 * @param {IContext} context 应用上下文
	 * @param {IParam} viewParams 视图参数
	 * @return {*}  {Promise<IOpenViewResult>}
	 * @memberof OpenViewHelper
	 */
	openTopDrawer(
		view: IViewDetail,
		context: IContext,
		viewParams: IParam
	): Promise<IOpenViewResult> {
		const drawerInstance = DrawerHelper.getInstance();
		return drawerInstance.openDrawer(view, context, viewParams, 'top');
	}

	/**
	 * @description 打开下方抽屉
	 * @param {IViewDetail} view 视图详情
	 * @param {IContext} context 应用上下文
	 * @param {IParam} viewParams 视图参数
	 * @return {*}  {Promise<IOpenViewResult>}
	 * @memberof OpenViewHelper
	 */
	openBottomDrawer(
		view: IViewDetail,
		context: IContext,
		viewParams: IParam
	): Promise<IOpenViewResult> {
		const drawerInstance = DrawerHelper.getInstance();
		return drawerInstance.openDrawer(view, context, viewParams, 'bottom');
	}

	/**
	 * @description 打开左侧抽屉
	 * @param {IViewDetail} view 视图详情
	 * @param {IContext} context 应用上下文
	 * @param {IParam} viewParams 视图参数
	 * @return {*}  {Promise<IOpenViewResult>}
	 * @memberof OpenViewHelper
	 */
	openLeftDrawer(
		view: IViewDetail,
		context: IContext,
		viewParams: IParam
	): Promise<IOpenViewResult> {
		const drawerInstance = DrawerHelper.getInstance();
		return drawerInstance.openDrawer(view, context, viewParams, 'left');
	}

	/**
	 * @description 打开右侧抽屉
	 * @param {IViewDetail} view 视图详情
	 * @param {IContext} context 应用上下文
	 * @param {IParam} viewParams 视图参数
	 * @return {*}  {Promise<IOpenViewResult>}
	 * @memberof OpenViewHelper
	 */
	openRightDrawer(
		view: IViewDetail,
		context: IContext,
		viewParams: IParam
	): Promise<IOpenViewResult> {
		const drawerInstance = DrawerHelper.getInstance();
		return drawerInstance.openDrawer(view, context, viewParams, 'right');
	}
}
